<?php

class Ads_Form_Search extends Zend_Form {

    protected $_translate = null;

    public function init() {
        $this->_translate = Zend_Registry::get('Zend_Translate');
        $cityDb = new Ads_Model_City();
        $stateDb = new Ads_Model_State();
        $zipDb = new Ads_Model_Zipcode();
        $settingDb = new Ads_Model_Rentsetting();

        //state
        $state = new Zend_Form_Element_Multiselect('state');
        $stateArray = $stateDb->fetchAll();
        $state->setLabel($this->_translate->_('select_area_location').':')
                ->setAttribs(array('title' => $this->_translate->_('select_area_location'), 'class' => 'span6'))
//                ->removeDecorator('Label')
                ->setDecorators(array(
                    'ViewHelper',
                    'Description',
                    'Errors'
                ))
                ->addValidator('Int');
        foreach ($stateArray as $value) {
            $state->addMultiOption($value->id, $value->name);
        }


        //zip
        $cityArray = $cityDb->fetchAll();
        $zipcode = new Zend_Form_Element_Multiselect('zipcode');
        $zipArray = $zipDb->fetchAll();
        $zipcode->setLabel($this->_translate->_('select_zip'))
                ->setAttribs(array('title' => $this->_translate->_('select_zip'), 'class' => 'span6'))
//                ->removeDecorator('Label')
                ->setDecorators(array(
                    'ViewHelper',
                    'Description',
                    'Errors'
                ))
                ->addValidator('Int');
        foreach ($zipArray as $value) {
            $zipId = $value->id;
            $zipCode = $value->codes;
            $cityId = $value->city_id;
            $cityName = $cityArray[$cityId - 1]['name'];
            $zipcode->addMultiOption($zipId, $zipCode . ' ' . $cityName);
        }

        //property type
        $propertyType = new Zend_Form_Element_Multiselect('propertyType');
        $proArray = $settingDb->getProprertyTypeArray()->property_type;
        $pkeys = array_keys($proArray);
        $propertyType->setLabel($this->_translate->_('select_property_type'))
                ->setAttribs(array('title' => $this->_translate->_('select_property_type'), 'class' => 'span6'))
                ->addValidator('Int')
                ->setDecorators(array(
                    'ViewHelper',
                    'Description',
                    'Errors'
                ))
//                ->addMultiOption(-1, 'Select Property Type')
                ->addFilter('HtmlEntities');
        for ($index = 0; $index < count($proArray); $index++) {
            $propertyType->addMultiOption($pkeys[$index], $proArray[$index]);
        }

        //rental period
        $rentalPeriod = new Zend_Form_Element_Multiselect('rentalPeriod');
        $rparray = $settingDb->getRentalPeriodArray()->rental_period;
        $rpkeys = array_keys($rparray);
        $rentalPeriod->setLabel($this->_translate->_('select_rental_period'))
                ->addValidator('Int')
                ->setAttribs(array('title' => $this->_translate->_('select_rental_period'), 'class' => 'span6'))
                ->setDecorators(array(
                    'ViewHelper',
                    'Description',
                    'Errors'
                ))
//                ->addMultiOption(-1, 'Select Rental Period')
                ->addFilter('HtmlEntities');
        for ($index1 = 0; $index1 < count($rparray); $index1++) {
            $rentalPeriod->addMultiOption($rpkeys[$index1], $rparray[$index1]);
        }

        //furnish type
        $furnishType = new Zend_Form_Element_Multiselect('furnishType');
        $furarray = $settingDb->getFurnishTypeArray()->furnish_type;
        $furkey = array_keys($furarray);
        $furnishType->setLabel($this->_translate->_('select_furnish_type'))
                ->setOptions(array('title' => $this->_translate->_('select_furnish_type'), 'class' => 'span6'))
                ->addValidator('Int')
                ->setDecorators(array(
                    'ViewHelper',
                    'Description',
                    'Errors'
                ))
//                ->addMultiOption(-1, 'Select Furnish Type')
                ->addFilter('HtmlEntities');
        for ($index3 = 0; $index3 < count($furarray); $index3++) {
            $furnishType->addMultiOption($furkey[$index3], $furarray[$index3]);
        }



        $rooms = new Zend_Form_Element_Multiselect('rooms');
        $rooms->setLabel($this->_translate->_('select_rooms'))
                ->setOptions(array('title' => $this->_translate->_('select_rooms'), 'class' => 'span6'))
                ->setDecorators(array(
                    'ViewHelper',
                    'Description',
                    'Errors',
                ))
                ->addMultiOptions(array(
                    1 => '1',
                    2 => '2',
                    3 => '3',
                    4 => '4',
                    5 => '5',
                    6 => '6',
                    7 => '7',
                    8 => '8',
                    9 => '9',
                    10 => '10+',
        ));

        $size1 = new Zend_Form_Element_Text('size1');
        $size1->setLabel( $this->_translate->_('min_size'))
                ->setDecorators(array(
                    'ViewHelper',
                    'Description',
                    'Errors',
                ))
                ->setAttribs(array('style' => 'width: 40%'))
                ->setValue(0)
                ->setOptions(array('title' =>  $this->_translate->_('min_size'), 'placeholder' => $this->_translate->_('min_size')));

        $size2 = new Zend_Form_Element_Text('size2');
        $size2->setLabel( $this->_translate->_('max_size'))
                ->setDecorators(array(
                    'ViewHelper',
                    'Description',
                    'Errors',
                ))
                ->setAttribs(array('style' => 'width: 40%'))
                ->setValue(0)
                ->setOptions(array('title' =>  $this->_translate->_('max_size'), 'placeholder' => $this->_translate->_('max_size')));

        $rent1 = new Zend_Form_Element_Text('rent1');
        $rent1->setLabel($this->_translate->_('min_rent'))
                ->setDecorators(array(
                    'ViewHelper',
                    'Description',
                    'Errors',
                ))
                ->setAttribs(array('style' => 'width: 40%'))
                ->setValue(0)
                ->setOptions(array('title' => $this->_translate->_('min_rent'), 'placeholder' => $this->_translate->_('min_rent')));

        $rent2 = new Zend_Form_Element_Text('rent2');
        $rent2->setLabel($this->_translate->_('max_rent'))
                ->setDecorators(array(
                    'ViewHelper',
                    'Description',
                    'Errors',
                ))
                ->setValue(0)
                ->setAttribs(array('style' => 'width: 40%'))
                ->setOptions(array('title' => $this->_translate->_('max_rent'), 'placeholder' => $this->_translate->_('max_rent')));


        $rent = new Zend_Form_Element_Select('rent');
        $rent->setLabel('Rent:')
                ->setOptions(array('title' => 'Select Rent'))
                ->setDecorators(array(
                    'ViewHelper',
                    'Description',
                    'Errors',
                ))
                ->addMultiOptions(array(
                    0 => 'Select Rent',
                    1 => '1-500',
                    2 => '501-1000',
                    3 => '1001-1500',
                    4 => '1501-2000',
                    5 => '2001-2500',
                    6 => '2501+',
        ));




        $this->addElements(array($state, $zipcode, $propertyType, $rentalPeriod, $furnishType, $rooms, $size1, $size2, $rent1, $rent2));
        $this->addDisplayGroup(array('state', 'zipcode', 'propertyType', 'rentalPeriod', 'furnishType', 'rooms', 'size1', 'size2', 'rent1', 'rent2'), 'region');
        $this->getDisplayGroup('region')
                ->setLegend('Search')
                ->setDecorators(array(
                    'FormElements',
                    'Fieldset',
                    array(array('fieldset' => 'HtmlTag'), array('tag' => 'div'))
        ));

//        $this->addElements(array($rooms));
//        $this->addDisplayGroup(array('rooms'),'roomss');
//        $this->getDisplayGroup('roomss')
//                ->setDecorators(array(
//                    'FormElements',
//                    'Fieldset',
//                    array('Label', array('tag' => 'label')),
//                    array(array('fieldset' => 'HtmlTag'), array('tag' => 'div','class'=>'span6'))
//                ));
//        $this->addElements(array($area));
//        $this->addDisplayGroup(array('area'),'areass');
//        $this->getDisplayGroup('areass')
//                ->setDecorators(array(
//                    'FormElements',
//                    'Fieldset',
//                    array('Label', array('tag' => 'label')),
//                    array(array('fieldset' => 'HtmlTag'), array('tag' => 'div'))
//                ));




        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel($this->_translate->_('search'))
                ->setDecorators(array(
                    'ViewHelper',
                    'Description',
                    'Errors',
//                    array(array('data' => 'HtmlTag'), array('tag' => 'td','colspan'=>2,'align'=>'center')),
//                    array(array('row' => 'HtmlTag'), array('tag' => 'tr'))
                ))
                ->setAttribs(array('class' => 'btn-success btn'));

        $this->addElements(array($submit));
    }

}

